package com.btshop.goods.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.btshop.goods.pojo.Brand;
import com.btshop.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import com.util.Page;
import com.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName BrandController
 * @Description 品牌控制层
 * @Author by nike
 * @Date 2020/7/14 22:18
 * @Version V1.0
 **/
@RestController
@RequestMapping("/brand")
@CrossOrigin
@Api(value = "品牌接口", tags = "品牌接口")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * @Description 查询品牌集合
     * @Date 2020/7/14 22:48
     * @Param []
     * @return com.util.Result<java.util.List<com.btshop.goods.pojo.Brand>>
     * @Author ld
     **/
    @ApiOperation(value = "查询品牌集合", notes = "查询品牌集合", response = Result.class)
    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brands = brandService.findAll();
        return Result.buildSuccess("查询品牌集合成功！", brands);
    }

    /**
     * @Description 根据id查询品牌
     * @Date 2020/7/14 22:47
     * @Param [id]
     * @return com.util.Result<com.btshop.goods.pojo.Brand>
     * @Author ld
     **/
    @ApiOperation(value = "根据id查询品牌", notes = "根据id查询品牌", response = Result.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping(value = "/{id}")
    public Result<Brand> findById(@PathVariable("id") Long id) {
        Brand brand = brandService.findById(id);
        return Result.buildSuccess("根据id查询品牌成功！", brand);
    }


    /**
     * @Description 品牌新增
     * @Date 2020/7/14 22:47
     * @Param [brand]
     * @return com.util.Result
     * @Author ld
     **/
    @ApiOperation(value = "品牌新增", notes = "品牌新增", response = Result.class)
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return Result.buildSuccess("品牌新增成功！");
    }

    /**
     * @description: 根据id更新品牌
     * @Param: [id, brand]
     * @return: com.util.Result
     * @author: 李东
     * @time: 2020-7-15 10:57
     **/
    @ApiOperation(value = "根据id更新品牌", tags = "根据id更新品牌", response = Result.class)
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键id"))
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody Brand brand) {
        brandService.update(id, brand);
        return Result.buildSuccess("品牌更新成功！");
    }

    /**
     * @description: 根据id删除品牌
     * @Param: [id]
     * @return: com.util.Result
     * @author: 李东
     * @time: 2020-7-15 17:27
     **/
    @ApiOperation(value = "根据id删除品牌", tags = "根据id删除品牌", response = Result.class)
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键id"))
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        brandService.delete(id);
        return Result.buildSuccess("品牌删除成功！");
    }

    /**
     * @description: 根据品牌信息多条件查询
     * @Param: [brand]
     * @return: com.util.Result<java.util.List<com.btshop.goods.pojo.Brand>>
     * @author: 李东
     * @time: 2020-7-15 17:35
     **/
    @ApiOperation(value = "根据品牌信息多条件查询", tags = "根据品牌信息多条件查询", response = Result.class)
    @PostMapping("/search")
    public Result<List<Brand>> search(@RequestBody Brand brand) {
        List<Brand> brands = brandService.search(brand);
        return Result.buildSuccess("多条件查询品牌成功！", brands);
    }

    /**
     * @description: 根据品牌信息多条件分页查询
     * @Param: [page, size, brand]
     * @return: com.util.Result<com.util.Page<com.btshop.goods.pojo.Brand>>
     * @author: 李东
     * @time: 2020-7-15 17:56
     **/
    @ApiOperation(value = "根据品牌信息多条件分页查询", tags = "根据品牌信息多条件查询", response = Result.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小")})
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> searchPage(@PathVariable("page") Integer page,
            @PathVariable("size") Integer size, @RequestBody Brand brand) {
        PageInfo<Brand> pageInfo = brandService.searchPage(page, size, brand);
        return Result.buildSuccess("根据品牌信息多条件分页查询成功！",pageInfo);
    }
}
